<template>
  <div class="container">
    <div class="app-container">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>试题录入</span>
        </div>
        <el-form ref="formRef" :model="formData" :rules="questionsRules" label-width="80px">
          <!--学科-->
          <el-form-item label="学科:" prop="subjectID">
            <el-select style="width: 400px" clearable @change="changeSubjects(formData.subjectID)" v-model="formData.subjectID" placeholder="请选择">
              <el-option v-for="item in Subjects" :key="item.id" :value="item.value" :label="item.label"> </el-option>
            </el-select>
          </el-form-item>
          <!--目录-->
          <el-form-item label="目录:" prop="catalogID">
            <el-select style="width: 400px" clearable v-model="formData.catalogID" placeholder="请选择">
              <el-option v-for="item in Dirctory" :key="item.value" :value="item.value" :label="item.label"> </el-option>
            </el-select>
          </el-form-item>
          <!--企业-->
          <el-form-item label="企业:" prop="enterpriseID">
            <el-select style="width: 400px" v-model="formData.enterpriseID" clearable placeholder="请选择">
              <el-option v-for="item in Companys" :key="item.id" :label="item.shortName" :value="item.id"> </el-option>
            </el-select>
          </el-form-item>
          <!--城市-->
          <el-form-item label="城市:" prop="city">
            <el-select style="width: 198px" v-model="formData.province" @change="citysList" clearable placeholder="请选择省份">
              <el-option v-for="item in citySelect.province" :key="item.id" :value="item"> </el-option> </el-select
            >&nbsp;&nbsp;
            <el-select style="width: 198px" clearable v-model="formData.city" placeholder="请选择城市">
              <el-option v-for="item in citySelect.cityDate" :key="item.id" :value="item"> </el-option>
            </el-select>
          </el-form-item>
          <!--方向-->
          <el-form-item label="方向:" prop="direction">
            <el-select style="width: 400px" clearable v-model="formData.direction" placeholder="请选择">
              <el-option v-for="item in directionlist" :key="item.id" :value="item" :label="item"> </el-option>
            </el-select>
          </el-form-item>
          <!--题型-->
          <el-form-item label="题型:" prop="questionType">
            <el-radio-group v-model="formData.questionType">
              <el-radio :label="item.value + ''" v-for="item in questionType" :key="item.value">{{ item.label }} </el-radio>
            </el-radio-group>
          </el-form-item>
          <!--难度-->
          <el-form-item label="难度:" prop="difficulty">
            <el-radio-group v-model="formData.difficulty">
              <el-radio :label="item.value + ''" v-for="item in difficulty" :key="item.value">
                {{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <!--题干-->
          <el-form-item label="题干:" prop="question">
            <quill-editor v-model="formData.question" :options="editorOption"> </quill-editor>
          </el-form-item>
          <!--选项-->
          <el-form-item label="选项:" v-if="formData.questionType !== '3'">
            <div class="choice" v-for="item in formData.options" :key="item.code">
              <el-radio v-if="formData.questionType === '1'" @change="changeRadio(item)" v-model="item.isRight" :label="true">{{ item.code }}： </el-radio>
              <el-checkbox v-if="formData.questionType === '2'" v-model="item.isRight" :label="true">{{ item.code }}： </el-checkbox>
              <el-input v-model="item.title" style="width:240px"></el-input>
              <el-upload
                class="avatar-uploader"
                action="https://jsonplaceholder.typicode.com/posts/"
                :show-file-list="false"
                :on-success="
                  (res, file) => {
                    uploadSuccess(item, res, file)
                  }
                "
              >
                <img v-if="item.img" :src="item.img" class="avatar" />
                <span>上传图片</span>
                <i class="el-icon-circle-close"></i>
              </el-upload>
            </div>
          </el-form-item>
          <el-form-item v-show="formData.questionType !== '3'">
            <el-button type="danger" :disabled="formData.questionType !== '2'" size="mini" @click="addOptions">+增加选项与答案 </el-button>
          </el-form-item>
          <!--解析视频-->
          <el-form-item label="视频解析:">
            <el-input style="width: 400px" v-model="formData.video" placeholder="请输入内容"></el-input>
          </el-form-item>
          <!--答案解析-->
          <el-form-item label="答案解析:" prop="answer">
            <quill-editor v-model="formData.answer" :options="editorOption"> </quill-editor>
            <!-- 富文本内容 -->
          </el-form-item>
          <!--题目备注-->
          <el-form-item label="题目备注:">
            <el-input style="width: 400px" type="textarea" placeholder="请输入内容" v-model="formData.textarea" show-word-limit> </el-input>
          </el-form-item>
          <!--试题标签-->
          <el-form-item label="试题标签:">
            <el-select style="width: 400px" v-model="formData.tags" multiple placeholder="请选择">
              <el-option v-for="item in tags" :key="item.value" :label="item.label" :value="item.value"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('formData')">立即创建</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
// 富文本
import { quillEditor } from 'vue-quill-editor'
// 学科接口
import { simple as getSubjects } from '@/api/hmmm/subjects'
// 目录接口
import { simple as getDirctory } from '@/api/hmmm/directorys'
// 企业接口
import { list as getCompanys } from '@/api/hmmm/companys'
// 获取城市的数据
import { provinces, citys } from '@/api/hmmm/citys'
// 题型 难度  方向
import { direction, questionType, difficulty } from '@/api/hmmm/constants'
// 试题
import { simple } from '@/api/hmmm/tags'
// 添加试题
import { add } from '@/api/hmmm/questions'
// 深拷贝
import _ from 'lodash'
export default {
  name: 'tryTyping',
  data() {
    return {
      // 提交表单git
      formData: {
        aa: '',
        subjectID: null, // 学科
        catalogID: null, // 目录
        enterpriseID: null, // 企业
        province: null, // 省份
        city: null, // 城市
        direction: null, // 方向
        // 选项
        options: [
          { code: 'A', title: '', img: '', isRight: false },
          { code: 'B', title: '', img: '', isRight: false },
          { code: 'C', title: '', img: '', isRight: false },
          { code: 'D', title: '', img: '', isRight: false }
        ],
        questionType: '1', // 题型
        video: '',
        difficulty: '1', // 难度
        tags: null, // 标签
        textarea: ''
      },
      // 学科的数据
      Subjects: [],
      // 目录的数据
      Dirctory: [],
      // 企业的数据
      Companys: [],
      // 省份
      citySelect: {
        province: [],
        cityDate: []
      },
      // 方向
      directionlist: direction,
      // 题型
      questionType,
      // 难度
      difficulty,
      // 题干
      question: null,
      // // 标签
      tags: [],
      // 富文本
      content: null,
      editorOption: {
        modules: {
          toolbar: [['bold', 'italic', 'underline', 'strike', 'image', { list: 'ordered' }, { list: 'bullet' }, 'blockquote', 'code-block']]
        },
        textarea: ''// 题目备注
      },
      // 预校验
      questionsRules: {
        // 表单校验
        subjectID: [{ required: true, message: '请选择学科', trigger: 'change' }],
        catalogID: [{ required: true, message: '请选择活动目录', trigger: 'change' }],
        enterpriseID: [{ required: true, message: '请选择企业', trigger: 'change' }],
        city: [{ required: true, message: '请选择城市', trigger: 'change' }],
        direction: [{ required: true, message: '请选择方向', trigger: 'change' }],
        questionType: [{ required: true, message: '请选择题型', trigger: 'change' }],
        difficulty: [{ required: true, message: '请选择难度', trigger: 'change' }],
        question: [{ required: true, message: '请选择题干', trigger: 'blur' }],
        answer: [{ required: true, message: '请选择解析', trigger: 'blur' }],
        tags: [{ required: true, message: '请选择标签', trigger: 'change' }]
      }
    }
  },
  // 创建完毕状态
  created() {
    //  获取试题
    this.getSubjects()
    // 企业
    this.getCompanys()
    // 城市
    this.getProvince()
  },
  // 子组件
  components: {
    quillEditor
  },
  // 方法
  methods: {
    // 获取学科的内容
    async getSubjects() {
      const { data } = await getSubjects()
      // console.log(data)
      this.Subjects = data
      // console.log(this.Subjects)
    },
    //  获取目录的内容
    async changeSubjects(subjectID) {
      console.log(subjectID)
      this.formData.catalogID = ''
      const { data } = await getDirctory({ subjectID: this.formData.subjectID })
      console.log(data)
      this.Dirctory = data
      console.log(this.Dirctory)
      const res = await simple()
      console.log(res.data)
      this.tags = res.data
    },
    //  获取企业的
    async getCompanys() {
      const { data } = await getCompanys()
      // console.log(data)
      this.Companys = data.items
      // console.log(this.Companys)
    },
    // 获取省份
    getProvince() {
      try {
        const res = provinces()
        // console.log(data)
        this.citySelect.province = res
        // console.log(this.citySelect.province)
      } catch (err) {
        console.log(err)
      }
    },
    // 城市
    citysList(e) {
      console.log(e)
      // 获取省份
      try {
        const data = citys(e)
        console.log(data)
        this.citySelect.cityDate = data
      } catch (err) {
        this.$messong.err('获取省份失败')
        // console.log(err)
      }
    },
    uploadSuccess(item, res, file) {
      item.img = URL.createObjectURL(file.raw)
    },
    // 题干
    // checkQustion () {
    //   this.$refs.form.validateField('stem')
    // },
    // 增加选项
    // 改变单选
    changeRadio(item) {
      this.formData.options.forEach(item => {
        item.isRight = false
      })
      item.isRight = true
    },
    // 添加题库
    addOptions() {
      this.formData.options.push({
        code: '',
        img: '',
        title: '',
        isRight: false
      })
    },
    // 表单校验
    submitForm() {
      this.$refs.formRef.validate(async valid => {
        if (!valid) return this.$message.error('请输入必填项')
        // console.log(this.formData)
        // const formdata = .formData
        // this.formData=formdata.toString()
        try {
          const form = _.cloneDeep(this.formData)
          form.tags = form.tags.toString()
          const res = await add(form)
          console.log(res)
          this.$message.success('提交成功')
        } catch (err) {
          console.log(err)
          this.$message.error('提交失败')
        }
      })
    }
  }
}
</script>
<style>
.option {
  width: 500px;
  position: relative;
  padding-top: 30px;
}

.right {
  position: absolute;
  bottom: -5px;
  right: 100px;
}

.wrong {
  position: absolute;
  top: -9px;
  right: -9px;
  font-size: 20px !important;
}

.btn {
  margin-top: 30px;
}

.el-upload--picture-card {
  width: 90px;
  height: 50px;
  line-height: 50px;
}

.ql-editor {
  min-height: 200px;
}
.avatar-uploader {
  width: 100px;
  height: 60px;
  line-height: 60px;
  border: 1px dashed #d9d9d9;
  cursor: pointer;
  border-radius: 6px;
  position: relative;
  display: inline-block;
  text-align: center;
  margin-left: 15px;
}
.el-icon-circle-close {
  position: absolute;
  top: 0;
  right: 0;
  transform: translate(50%, -50%);
  font-size: 18px;
  color: #999;
}
</style>
